﻿using System.ComponentModel;

// Author: Miroslav Matula m.matula@alutam.cz 
// Date : 31/8/2010

namespace System.Windows.Controls.DGTB
{
#region -- TemplatePart --
			[TemplatePart(Name = "Chrome", Type = typeof(FrameworkElement))]
	[TemplatePart(Name = "Overlay", Type = typeof(Panel))]
	[TemplateVisualState(Name = "Open", GroupName = "WindowStates")]
	[TemplatePart(Name = "ContentRoot", Type = typeof(FrameworkElement))]
	[TemplatePart(Name = "CloseButton", Type = typeof(Primitives.ButtonBase))]
	[TemplatePart(Name = "Root", Type = typeof(FrameworkElement))]
	[TemplateVisualState(Name = "Closed", GroupName = "WindowStates")]
	[TemplatePart(Name = "ContentPresenter", Type = typeof(FrameworkElement))]
	[TemplatePart(Name = DataFormWindow_elementBusy, Type = typeof(FrameworkElement))]

	#endregion	
	public  class DataFormWindow : ChildWindow, IDataGridToolBar_DataFormWindow
	{
		// Parts constants
		private const string DataFormWindow_elementBusy = "elementBusy";

		#region ctor
		public DataFormWindow()
		{
			if(Application.Current.RootVisual is FrameworkElement) {
				this.Language = ((FrameworkElement)Application.Current.RootVisual).Language;
			}
			this.DefaultStyleKey = typeof(DataFormWindow);
			base.Closing += new EventHandler<CancelEventArgs>(DataFormWindow_Closing);
		}

		#endregion

		#region -- Override --
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			_elementBusy = GetTemplateChild(DataFormWindow_elementBusy) as FrameworkElement;
			if(Busy) _elementBusy.Visibility = Windows.Visibility.Visible; else _elementBusy.Visibility = Windows.Visibility.Collapsed;
		}

		#endregion

		public virtual object CreateNewItem(){
			throw new Exception("You have to override this method.");
		}
		public virtual void CancelEdit(){
		}
		public virtual bool ValidateItem(){
			return true;
		}

		public bool NewItem {get; private set;}
		private FrameworkElement _elementBusy;
		private int _busyCnt=0;
		protected int BusyCnt
		{
			get { return _busyCnt; }
		set{
			_busyCnt = value;
			if(_elementBusy!=null) if(value>0)_elementBusy.Visibility=Windows.Visibility.Visible; else _elementBusy.Visibility=Windows.Visibility.Collapsed;
		}}
		public bool Busy
		{
			get { return _busyCnt>0; }
		}


		
		public object ItemsSource { get; set; }
		private object _curentItem;
		public object CurrentItem
		{
			get { return _curentItem; }
			protected set
			{
				if(_curentItem == value) return;
				_curentItem = value;
				SetDataContect(_curentItem);
			}
		}
		
		public Action<bool> CallBackDataChanged { get; set; }

		public event EventHandler DataChanged;

		public void OnDataChanged()
		{
			if(DataChanged != null) DataChanged(CurrentItem, EventArgs.Empty);
		}

		public virtual void SetDataContect(object item)
		{
			this.DataContext = item;
		}

		public virtual void AddNewItem()
		{
			DialogResult = null;
			CurrentItem = CreateNewItem();
			NewItem = true;
			BusyCnt = 0;
		}

		//void IDataGridToolBar_DataFormWindow.EditItem(object item)
		//{
		//  EditItemInternal(item);
		//}
		public void EditItem(object item)
		{
			DialogResult = null;
			BusyCnt = 0;
			NewItem = false;
			EditItem(item, (p) => {
				CurrentItem = item;
				BusyCnt--;
			});
			BusyCnt--;
		}

		public virtual void EditItem(object item, Action<bool> callback)
		{
			callback(true);
		}


		public virtual void SubmitChanges(bool isNewItem, Action<bool> callback){
			callback(true);
		}

		public void SaveItemInternal()
		{
			BusyCnt++;
			bool tt = NewItem;
			if(!ValidateItem()) throw new Exception("Item is not valid");
			SubmitChanges(tt, (ok) => {
				BusyCnt--;
				if(ok) {
					OnDataChanged();
					NewItem = false;
				}
			});
			/*
			if(NewItem) {
				NewItem = false;
				AddItem((p) => {
					if(p) {
 						// OK
						SubmitChanges(tt, (ok) => {
							BusyCnt--;
							if(ok) OnDataChanged();
						});
					} else {
						//Error
						BusyCnt--;
					}
				});
			} else {
				SubmitChanges(tt, (ok) => {
					BusyCnt--;
					if(ok) OnDataChanged();
				});
			}
			 */ 
		}


		void DataFormWindow_Closing(object sender, CancelEventArgs e)
		{
			if(DialogResult == null) {
				// Close with button in Top Right
				DialogResult = false;
				CancelEdit();
			}
		}

		public void CommitEditAndCloseWindow()
		{
			if(!ValidateItem()) return;
			CallBackDataChanged = (ok) => {
				CallBackDataChanged = null;
				BusyCnt--;
				if(ok) {
					this.DialogResult = true;
					Close();
				} else {
					//Todo:vypsat zprávu
					//if(CurrentItem != null) ((IEditableObject)CurrentItem).BeginEdit();
				}
			};
			SaveItemInternal();
			BusyCnt++;
		}

		public void CancelEditAndCloseWindow()
		{
			CancelEdit();
			this.DialogResult = false;
			Close();
		}


	}
}
